#include "packconfig.h"
#include <QJsonArray>
#include <QFile>
#include <QDir>
#include <QRegularExpression>
#include <QDebug>

PackConfig::PackConfig() {
    // 设置默认值
    name = "unnamed";
    version = "1.0.0";
    description = "Pack Tool";
    output = "output.zip";
    output_dir = ".";
    source_dir = ".";
}

bool PackConfig::fromJson(const QJsonObject& json) {
    try {
        name = json["name"].toString();
        version = json["version"].toString();
        description = json["description"].toString();
        output = json["output"].toString();
        output_dir = json["output_dir"].toString();
        source_dir = json["source_dir"].toString();
        
        // 读取文件列表
        files.clear();
        QJsonArray filesArray = json["files"].toArray();
        for (const auto& value : filesArray) {
            files.append(value.toString());
        }
        
        // 读取文件夹列表
        folders.clear();
        QJsonArray foldersArray = json["folders"].toArray();
        for (const auto& value : foldersArray) {
            folders.append(value.toString());
        }
        
        return true;
    } catch (...) {
        qDebug() << "Failed to parse JSON config";
        return false;
    }
}

QJsonObject PackConfig::toJson() const {
    QJsonObject json;
    json["name"] = name;
    json["version"] = version;  
    json["description"] = description;
    json["output"] = output;
    json["output_dir"] = output_dir;
    json["source_dir"] = source_dir;
    
    // 添加文件列表
    QJsonArray filesArray;
    for (const QString& file : files) {
        filesArray.append(file);
    }
    json["files"] = filesArray;
    
    // 添加文件夹列表
    QJsonArray foldersArray;
    for (const QString& folder : folders) {
        foldersArray.append(folder);
    }
    json["folders"] = foldersArray;
    
    return json;
}

bool PackConfig::loadFromFile(const QString& filePath) {
    QFile file(filePath);
    if (!file.open(QIODevice::ReadOnly)) {
        qDebug() << "Cannot open config file:" << filePath;
        return false;
    }
    
    QByteArray data = file.readAll();
    QJsonDocument doc = QJsonDocument::fromJson(data);
    
    if (!doc.isObject()) {
        qDebug() << "Invalid JSON format";
        return false;
    }
    
    return fromJson(doc.object());
}

bool PackConfig::saveToFile(const QString& filePath) const {
    QJsonDocument doc(toJson());
    
    QFile file(filePath);
    if (!file.open(QIODevice::WriteOnly)) {
        qDebug() << "Cannot write config file:" << filePath;
        return false;
    }
    
    file.write(doc.toJson());
    return true;
}

bool PackConfig::isValid() const {
    // 检查版本格式
    QRegularExpression versionRegex(R"(^\d+\.\d+\.\d+$)");
    if (!versionRegex.match(version).hasMatch()) {
        qDebug() << "Invalid version format:" << version;
        return false;
    }
    
    // 检查必要字段
    if (name.isEmpty() || output.isEmpty()) {
        qDebug() << "Missing required fields";
        return false;
    }
    
    // 检查是否至少有文件或文件夹
    if (files.isEmpty() && folders.isEmpty()) {
        qDebug() << "No files or folders specified";
        return false;
    }
    
    return true;
}
